const socketIo = require('socket.io')

class SocketManager {
  constructor(server, config = {}) {
    this.io = socketIo(server, config)
    this.io.on('connection', (socket) => {
      console.log('a user connected', socket.id)
      socket.on('disconnect', () => {
        console.log('user disconnected', socket.id)
      })
      //返回用户链接id
      socket.emit('id', socket.id)
      socket.on('chat message', (msg) => {
        console.log('message: ' + msg)
        this.io.emit('chat message', msg)
      })
    })
  }

  emit(eventName, eventData) {
    this.io.emit(eventName, eventData)
  }
}

module.exports = SocketManager
